package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.CharacterClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventListener;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Role;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Stats;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.managers.StoreManager;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CharInfoPanel extends JPanel implements GraphicEventListener {
	
	 private JLabel[] statsNamesLabels = new JLabel[Stats.TOTAL_STATISTICS];
	 private JLabel[] charStatsLabels = new JLabel[Stats.TOTAL_STATISTICS];
	 private JLabel avatarIcon = new JLabel();
	
	
	 protected GraphicEventManager gem;
	 protected StoreManager storeManager;
	 protected HashMap<String,String> settings;
	 protected OverallGUILayoutFactory guiCreator = OverallGUILayoutFactory.getInstance();
	 protected ImageCache imgCache;
	 protected GridBagConstraints gbc = new GridBagConstraints();
	 protected void setListeners() {
		 this.gem.addListener(this, CharacterClickEvent.class);
	}
	 
	protected CharInfoPanel(final GraphicEventManager gem, final StoreManager storeManager, HashMap<String,String> settings,
			 ImageCache imgCache) {
		this.gem = gem;
		this.storeManager = storeManager;
		this.settings = settings;
		this.imgCache = imgCache;
		
		setListeners();
		this.setOpaque(false);
		this.setLayout(new GridBagLayout());
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		this.add(avatarIcon, gbc);
		
		
		for(int i = 0; i < Stats.TOTAL_STATISTICS;i++){
			gbc.gridx = 0;
			gbc.gridy = i+1;
			gbc.anchor = GridBagConstraints.FIRST_LINE_START;
			statsNamesLabels[i] = new JLabel(Stats.statsKeyString(i).toUpperCase());
			statsNamesLabels[i].setForeground(Color.decode(StorePanel.HASH+settings.get(StorePanel.COLOR_NEUTRAL)));
			statsNamesLabels[i].setBackground(new Color(255,255,255,100));
			statsNamesLabels[i].setOpaque(true);
			try {
				statsNamesLabels[i].setFont(guiCreator.getParagraphFont().deriveFont(Font.BOLD,Integer.parseInt(settings.get(StorePanel.STATS_NAME_TEXT_SIZE)) ));
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			}
			statsNamesLabels[i].setPreferredSize(new Dimension(Integer.parseInt(settings.get(StorePanel.STATS_NAME_WIDTH)), Integer.parseInt(settings.get(StorePanel.STATS_JLABEL_HEIGHT))));
			statsNamesLabels[i].setHorizontalAlignment(JLabel.RIGHT);
			this.add(statsNamesLabels[i], gbc);
			gbc.gridx = 1;
			gbc.anchor = GridBagConstraints.FIRST_LINE_END;
			charStatsLabels[i] = new JLabel();
			charStatsLabels[i].setPreferredSize(guiCreator.getParsedDimension(settings.get(StorePanel.STATS_VALUE_WIDTH), settings.get(StorePanel.STATS_JLABEL_HEIGHT)));
			charStatsLabels[i].setHorizontalAlignment(JLabel.RIGHT);
			charStatsLabels[i].setOpaque(true);
			this.add(charStatsLabels[i] , gbc);
		}
		
	}

	@Override
	public void notify(GraphicEvent event) {
		
		if( event.getClass() == CharacterClickEvent.class) {
			
			CharacterClickEvent cce= (CharacterClickEvent) event;
			printCharStats(cce.getClickedChar().getStats());
			repaint();
		} 
	}

	protected void printCharStats(Role charStats){
		avatarIcon.setIcon(new ImageIcon(imgCache.getImage(settings.get(StorePanel.AVATAR_IMAGE_PATH)+charStats.getName()+settings.get(StorePanel.AVATAR_IMAGE_EXTENSION))));
		avatarIcon.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.DARK_GRAY.brighter(),2), BorderFactory.createLineBorder(Color.LIGHT_GRAY,2)));
		for(int i = 0; i<Stats.TOTAL_STATISTICS; i++) {
			charStatsLabels[i].setText(Integer.toString((int)charStats.getCharacteristics().getStats(i))+StorePanel.SPACE);
			charStatsLabels[i].setFont(guiCreator.getHeaderFont().deriveFont(Float.parseFloat( settings.get(StorePanel.STATS_VALUE_TEXT_SIZE))));
			charStatsLabels[i].setForeground(Color.decode(StorePanel.HASH+settings.get(StorePanel.COLOR_NEUTRAL)));
			charStatsLabels[i].setBackground(new Color(255,255,255,100));
	
	
		}
		
	}
	
}
	
